package com.bona.service.common;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bona.dao.common.BaseDao;
import com.bona.util.common.StringUtil;
import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

public abstract class BaseService {
	
	public abstract BaseDao getDao();
	
	public static final String OBJ_KEY = "model";
	
	/**
	 * 添加记录
	 * @param objs
	 * @return
	 */
	public <T> Integer add(T obj) {
		
		return getDao().add(obj);
	}
	
	/**
	 * 添加记录
	 * @param objs
	 * @return
	 */
	public Integer addBatch(Object... objs) {
		
		List<Object> list = Arrays.asList(objs);
		return getDao().addBatch(list);
	}
	
	/**
	 * 修改记录
	 * @param obj
	 * @return
	 */
	public Integer update(Object obj) {
		
		return getDao().update(obj);
	}
	
	/**
	 * 删除记录
	 * @param objs
	 * @return
	 */
	public Integer delete(Object obj) {
		return getDao().delete(obj);
	}
	
	/**
	 * 删除记录
	 * @param objs
	 * @return
	 */
	public Integer deleteBatch(Object... objs) {
		return getDao().deleteBatch(Arrays.asList(objs));
	}
	
	/**
	 * 查询记录
	 * @param obj
	 * @return
	 */
	public <T> T query(Object obj) {
		return getDao().query(obj);
	}
	
	/**
	 * 获取记录数
	 * @param model
	 * @return
	 */
	public Integer queryByCount(Object model) {
		
		Integer count = getDao().queryByCount(model);
		return count;
	}

	/**
	 * 获取分页数据
	 * @param model
	 * @return
	 */
	public <T> List<T> queryByPage(Object model) {
		
		List<T> ts = null;
		try {
			PageBounds pageBounds = new PageBounds();
		    Map<String, Object> map = new HashMap<String, Object>(); 
		    BeanInfo beanInfo = Introspector.getBeanInfo(model.getClass()); 
		    PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors(); 
	        for (int i = 0; i< propertyDescriptors.length; i++) { 
	            PropertyDescriptor descriptor = propertyDescriptors[i]; 
	            String propertyName = descriptor.getName(); 
	            if (propertyName.equals("class")) continue;
                Method readMethod = descriptor.getReadMethod(); 
                Object result = readMethod.invoke(model, new Object[0]); 
                map.put(propertyName, result);
	        } 
	        int page = Integer.valueOf(String.valueOf(map.get("page")==null? "0": map.get("page")));
	        int rows = Integer.valueOf(String.valueOf(map.get("rows")==null? "0": map.get("rows")));
	        String sort = String.valueOf(map.get("sort")==null? "": map.get("sort"));
	        if (StringUtil.isNotEmpty(sort)) {
	        	String order = String.valueOf(map.get("order")==null? "": map.get("order"));
	        	if (StringUtil.isNotEmpty(order)) {
	        		sort = sort+"."+order;
	        	}
	        	pageBounds.setOrders(Order.formString(sort));
	        }
	        pageBounds.setPage(page);
	        pageBounds.setLimit(rows);
			ts = getDao().queryByPage(map, pageBounds);
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return ts==null? new ArrayList<T>(): ts;
	}
}
